package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:
 * @Title: ShoppingCartController
 * @Package com.itheima.reggie.controller
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/16 14:14
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private HttpSession session;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> getList() {
        Long user = (Long) session.getAttribute("User");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @PostMapping("add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        Long user = (Long) session.getAttribute("User");
        //判断是添加套餐还是菜品
        if (shoppingCart.getSetmealId() != null) {
            //添加的套餐，进来先判断是否是第一次添加
            //查询购物车列表是否有当前套餐
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getUserId, user).eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            int count = shoppingCartService.count(lqw);
            if (count == 0) {
                //说明是第一次添加
                shoppingCart.setUserId(user);
                shoppingCartService.save(shoppingCart);
                return R.success(shoppingCart);
            } else {
                //不是第一次添加
                ShoppingCart one = shoppingCartService.getOne(lqw);
                Integer number = one.getNumber();
                LambdaUpdateWrapper<ShoppingCart> lqw2 = new LambdaUpdateWrapper<>();
                lqw2.eq(ShoppingCart::getUserId, user).eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
                lqw2.set(ShoppingCart::getNumber, number + 1);
                shoppingCartService.update(lqw2);
                ShoppingCart one2 = shoppingCartService.getOne(lqw);
                return R.success(one2);
            }
        } else {
            //添加的菜品，进来先判断是否是第一次添加
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getUserId, user).eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            int count = shoppingCartService.count(lqw);
            if (count == 0) {
                //第一次添加
                shoppingCart.setUserId(user);
                shoppingCartService.save(shoppingCart);
                return R.success(shoppingCart);
            } else {
                //不是第一次添加
                ShoppingCart one = shoppingCartService.getOne(lqw);
                Integer number = one.getNumber();
                LambdaUpdateWrapper<ShoppingCart> lqw2 = new LambdaUpdateWrapper<>();
                lqw2.eq(ShoppingCart::getUserId, user).eq(ShoppingCart::getDishId, shoppingCart.getDishId());
                lqw2.set(ShoppingCart::getNumber, number + 1);
                shoppingCartService.update(lqw2);
                ShoppingCart one2 = shoppingCartService.getOne(lqw);
                return R.success(one2);
            }
        }
    }

    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
//菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long userId = (Long) session.getAttribute("User");
        qw.eq(ShoppingCart::getUserId, userId);//where user_id =?
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {//是菜品
            qw.eq(ShoppingCart::getDishId, dishId);// dish_id = ?
        } else {// 是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());// setmeal_id = ?
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        // number -1 然后操作：
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0) {
            // >0  update更新 update set number=? where id = ?
            shoppingCartService.updateById(one);
        } else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    @DeleteMapping("clean")
    public R<String> delectAll() {
        Long user = (Long) session.getAttribute("User");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        shoppingCartService.remove(lqw);
        return R.success("清空成功");
    }
}
